﻿using System;
using System.Linq;
using System.Web.Mvc;
using NerdDinner.Helpers;
using NerdDinner.Models;

namespace NerdDinner.Controllers
{
    public class DinnersController : Controller
    {
        private readonly IDinnerRepository dinnerRepository;

        //public DinnersController() : this(new DinnerRepository())
        //{
        //}

        public DinnersController(IDinnerRepository repository)
        {
            dinnerRepository = repository;
        }

        public ActionResult Index(int page = 0)
        {
            IQueryable<Dinner> dinners = dinnerRepository.FindUpcomingDinners();
            var paginatedDinners = new PaginatedList<Dinner>(dinners, page, 10);
            return View("Index", paginatedDinners);
        }

        public ActionResult Find()
        {
            return View("Find");
        }

        public ActionResult Details(int id)
        {
            Dinner dinner = dinnerRepository.GetDinner(id);
            if (dinner == null)
            {
                return View("NotFound");
            }
            else
            {
                return View("Details", dinner);
            }
        }

        public ActionResult Delete(int id)
        {
            Dinner dinner = dinnerRepository.GetDinner(id);
            if (!dinner.IsHostedBy(User.Identity.Name))
            {
                return View("InvalidOwner");
            }
            if (dinner == null)
                return View("NotFound");
            else
                return View(dinner);
        }

        [Authorize]
        public ActionResult Edit(int id)
        {
            Dinner dinner = dinnerRepository.GetDinner(id);
            if (!dinner.IsHostedBy(User.Identity.Name))
            {
                return View("InvalidOwner");
            }
            return View(new DinnerFormViewModel(dinner));
        }

        [HttpPost, Authorize]
        public ActionResult Edit(int id, FormCollection formValues)
        {
            // Retrieve existing dinner
            Dinner dinner = dinnerRepository.GetDinner(id);
            if (!dinner.IsHostedBy(User.Identity.Name))
            {
                return View("InvalidOwner");
            }
            // Update dinner with form posted values after validation
            if (TryUpdateModel(dinner, "Dinner"))
            {
                // Persist changes back to database
                dinnerRepository.Save();
                // Perform HTTP redirect to details page for the saved Dinner
                return RedirectToAction("Details", new {id = dinner.DinnerID});
            }
            return View(new DinnerFormViewModel(dinner));
        }

        [Authorize]
        public ActionResult Create()
        {
            var dinner = new Dinner
                             {
                                 EventDate = DateTime.Now.AddDays(7)
                             };
            return View(new DinnerFormViewModel(dinner));
        }

        [HttpPost, Authorize]
        public ActionResult Create(Dinner dinner)
        {
            if (ModelState.IsValid)
            {
                dinner.HostedBy = User.Identity.Name;
                var rsvp = new RSVP();
                rsvp.AttendeeName = User.Identity.Name;
                dinner.RSVPs.Add(rsvp);
                dinnerRepository.Add(dinner);
                dinnerRepository.Save();
                return RedirectToAction("Details", new {id = dinner.DinnerID});
            }
            return View(new DinnerFormViewModel(dinner));
        }


        [HttpPost]
        public ActionResult Delete(int id, string confirmButton)
        {
            Dinner dinner = dinnerRepository.GetDinner(id);
            if (!dinner.IsHostedBy(User.Identity.Name))
            {
                return View("InvalidOwner");
            }
            if (dinner == null)
                return View("NotFound");
            dinnerRepository.Delete(dinner);
            dinnerRepository.Save();
            return View("Deleted");
        }
    }
}